﻿<%@ WebHandler Language="C#" Class="GetAllProductType" %>

using System;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Text;
//ADD BY DZT
//System.Web.SessionState.IReadOnlySessionState 增加这个接口类，SESSION可以直接访问
public class GetAllProductType : IHttpHandler, System.Web.SessionState.IReadOnlySessionState 
{

    OTS.BLL.ProductTypeBLL dll = new OTS.BLL.ProductTypeBLL();
    public void ProcessRequest(HttpContext context)
    {
        //检验是否登录
        if (!OTS.BaseLogin.validataModule(context, (int)OTS.enumSysRight.ProductTypeManager))
        {
            context.Response.ContentType = "text/json";
            context.Response.Write("非法操作！");
            return;
        }
        
        string id = context.Request["id"];
        if (null == id || "".Equals(id) || "root".Equals(id)) id = "1";
        List<OTS.Model.product_type> list = dll.GetModelList(" parentid=" + id + "");
        string ss = "[" + getAllTypeString(list) + "]";
        context.Response.ContentType = "text/json";
        context.Response.Write(ss);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }


    public static string getAllTypeString(List<OTS.Model.product_type> list)
    {
        StringBuilder sb = new StringBuilder();
        string str = "";
        if (list == null || list.Count < 1)
        {
            sb.Append("");
        }
        else
        {
            int count = list.Count;
            for (int i = 0; i < count; i++)
            {
                OTS.Model.product_type node = list[i];
                bool haveChildren = true;
                string chs = "false";  //默认不是叶子节点
                //string chs = "true";
                if (node.children == null || node.children.Count < 1)
                {
                    chs = "true";
                    haveChildren = false;
                }

                if (!haveChildren)
                    sb.Append("{\"children\":[],\"id\":" + node.typeId + ",\"leaf\":" + chs + ",\"text\":\"" + node.typeName + "\"},");
                else
                    sb.Append("{\"children\":[" + getAllTypeString(node.children) + "],\"id\":" + node.typeId + ",\"leaf\":" + chs + ",\"text\":\"" + node.typeName + "\"},");//有子节点


            }
            str = sb.ToString();
            if (str.EndsWith(","))
            {
                str = str.Substring(0, str.Length - 1);
            }

        }
        return str;
    }

}